import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../home/views/home_view.dart';
import '../../home_hd/views/home_hd_view.dart';
import '../../message/views/message_view.dart';
import '../../profile/views/profile_view.dart';
import '../../workbench/views/workbench_view.dart';
import '../controllers/main_tab_controller.dart';

class MainTabView extends GetView<MainTabController> {
  const MainTabView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody(),
      bottomNavigationBar: Obx(() => _buildBottomNavigationBar()),
    );
  }

  Widget _buildBody() {
    // 使用Obx响应式监听当前索引变化
    return Obx(() {
      // 根据当前索引返回对应的页面
      switch (controller.currentIndex.value) {
        case 0:
          if (controller.isHuodaiYunshu.isTrue) {
            return const HomeHdView();
          }
          return const HomeView();
        case 1:
          return const WorkbenchView();
        case 2:
          return const MessageView();
        case 3:
          return const ProfileView();
        default:
          return const HomeView();
      }
    });
  }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      iconSize: 20,
      currentIndex: controller.currentIndex.value,
      onTap: controller.changeTabIndex,
      items: [
        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage('assets/images/tabbar/tabbar_home.png')),
          activeIcon: ImageIcon(AssetImage('assets/images/tabbar/tabbar_home_selected.png')),
          label: '首页'.tr,
        ),
        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage('assets/images/tabbar/tabbar_work.png')),
          activeIcon: ImageIcon(AssetImage('assets/images/tabbar/tabbar_work_selected.png')),
          label: '工作台'.tr,
        ),
        BottomNavigationBarItem(
          icon: ImageIcon(
            AssetImage('assets/images/tabbar/tabbar_message.png')),
          activeIcon: ImageIcon(AssetImage('assets/images/tabbar/tabbar_message_selected.png')),
          label: '消息'.tr,
        ),
        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage('assets/images/tabbar/tabbar_me.png')),
          activeIcon: ImageIcon(AssetImage('assets/images/tabbar/tabbar_me_selected.png')),
          label: '我的'.tr,
        ),
      ],
    );
  }
}
